// 抽样校验 level_experience 与文档 doc/训练经验.md 的标准经验是否一致
// 规则：perHour = 文档(训练10小时总经验)/11；standard = perHour / 倍率(四舍五入, 最小1)

const mysql = require('mysql2/promise');
const fs = require('fs');
const path = require('path');
const config = require('./database-config.js');

function getMultiplier(level) {
    if (level >= 1 && level <= 40) return 150;
    if (level >= 41 && level <= 70) return 200;
    return 300;
}

function loadDocMap() {
    const docPath = path.join(__dirname, 'doc/训练经验.md');
    const lines = fs.readFileSync(docPath, 'utf8').split(/\r?\n/);
    const map = new Map();
    for (const line of lines) {
        const parts = line.split(',');
        if (parts.length >= 3 && /^\d+$/.test(parts[0]) && /^\d+/.test(parts[2])) {
            const level = parseInt(parts[0], 10);
            const total10h = parseInt(parts[2], 10);
            map.set(level, total10h);
        }
    }
    return map;
}

async function main() {
    const conn = await mysql.createConnection(config.source);
    try {
        const pick = [15, 30, 45, 60, 75, 90, 105, 120, 135, 150];
        const docMap = loadDocMap();
        console.log('🔍 抽样校验等级: ', pick.join(','));
        for (const level of pick) {
            const [rows] = await conn.execute(
                'SELECT experience FROM level_experience WHERE level=?',
                [level]
            );
            const exp = rows && rows[0] ? Number(rows[0].experience) : null;
            const total10h = docMap.get(level) ?? null;
            const perHour = total10h != null ? Math.floor(total10h / 11) : null;
            const mult = getMultiplier(level);
            const expected = perHour != null ? Math.max(1, Math.round(perHour / mult)) : null;
            const ok = exp === expected;
            console.log(
                `Lv${level}: 表=${exp}, 期望=${expected}, ${ok ? '✅ 匹配' : '❌ 不匹配'} (每小时=${perHour}, 倍率=${mult})`
            );
        }
    } catch (e) {
        console.error('❌ 校验失败:', e.message || e);
        process.exitCode = 1;
    } finally {
        await conn.end();
    }
}

if (require.main === module) {
    main();
}




